package com.cskaoyan;


import com.mysql.jdbc.Driver;

import java.sql.*;

/**
 * 创建日期: 2022/04/20 11:41
 *
 * @author ciggar
 */
public class MainForQuery2 {

    static String url = "jdbc:mysql://localhost:3306/39th?useSSL=false&characterEncoding=utf8";
    static String username = "root";
    static String password = "123456";

    public static void main(String[] args) throws SQLException {

        // 0. 注册驱动 (加载驱动程序)
        DriverManager.registerDriver(new Driver());


        // 1. 建立网络连接  Connection对象表示网络连接
        Connection connection = DriverManager.getConnection(url,username,password);

        // 3. 获取Statement对象
        Statement statement = connection.createStatement();

        // 4. 执行SQL语句，获取结果集
        ResultSet resultSet = statement.executeQuery("select * from user where id > 1");

//        for(;;){
//
//            if (resultSet.next()) {
//                int id = resultSet.getInt("id");
//                String name = resultSet.getString("name");
//                Date birthday = resultSet.getDate("birthday");
//
//                System.out.println("id:" + id + ", name:" + name +", birthday:" + birthday);
//            }else {
//
//                break;
//
//            }
//        }

        while (resultSet.next()) {

                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                Date birthday = resultSet.getDate("birthday");

                System.out.println("id:" + id + ", name:" + name +", birthday:" + birthday);
        }



        // 6. 断开连接
        resultSet.close();
        statement.close();
        connection.close();

    }
}
